/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
 
db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.dialogs;

import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author  klevgert
 */
public class Db4oNewDatabaseDialog extends javax.swing.JDialog {
  /** A return status code - returned if Cancel button has been pressed */
  public static final int RET_CANCEL = 0;
  /** A return status code - returned if OK button has been pressed */
  public static final int RET_OK = 1;

  public static final int TYPE_DATABASE_FILE = 0;
  public static final int TYPE_DATABASE_SERVER = 1;

  private String name;
  private String file;
  private String host;
  private int port;
  private String user;
  private char[] password;
  private int databaseType = 0;
    
  /**
   * Creates new form Db4oNewDatabaseDialog
   */
  public Db4oNewDatabaseDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    
    this.setLocationRelativeTo(null);

  }
  
  /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
  public int getReturnStatus() {
    return returnStatus;
  }

  public String getName() {
    return name;
  }

  public String getFile() {
    return file;
  }

  public String getHost() {
    return host;
  }

  public int getPort() {
    return port;
  }

  public String getUser() {
    return user;
  }

  public char[] getPassword() {
    return password;
  }

  public int getDatabaseType() {
    return databaseType;
  }
  
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  private void initComponents() {
    databaseTypeGroup = new javax.swing.ButtonGroup();
    okButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();
    lblName = new javax.swing.JLabel();
    txtName = new javax.swing.JTextField();
    radDatabaseFile = new javax.swing.JRadioButton();
    pnlDatabaseFile = new javax.swing.JPanel();
    lblFilename = new javax.swing.JLabel();
    txtFilename = new javax.swing.JTextField();
    btnBrowseFile = new javax.swing.JButton();
    radDatabaseServer = new javax.swing.JRadioButton();
    pnlDatabaseServer = new javax.swing.JPanel();
    lblHost = new javax.swing.JLabel();
    txtHost = new javax.swing.JTextField();
    lblPort = new javax.swing.JLabel();
    txtPort = new javax.swing.JTextField();
    lblUser = new javax.swing.JLabel();
    txtUser = new javax.swing.JTextField();
    lblPassword = new javax.swing.JLabel();
    txtPassword = new javax.swing.JPasswordField();

    setTitle("Add a Db4o Database Connection");
    setLocationByPlatform(true);
    setModal(true);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        closeDialog(evt);
      }
    });

    okButton.setText("OK");
    okButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okButtonActionPerformed(evt);
      }
    });

    cancelButton.setText("Cancel");
    cancelButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelButtonActionPerformed(evt);
      }
    });

    lblName.setText("Name:");

    databaseTypeGroup.add(radDatabaseFile);
    radDatabaseFile.setSelected(true);
    radDatabaseFile.setText("Database File");
    radDatabaseFile.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    radDatabaseFile.setMargin(new java.awt.Insets(0, 0, 0, 0));
    radDatabaseFile.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        radDatabaseFileActionPerformed(evt);
      }
    });

    lblFilename.setText("File Name:");

    btnBrowseFile.setText("...");
    btnBrowseFile.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnBrowseFileActionPerformed(evt);
      }
    });

    org.jdesktop.layout.GroupLayout pnlDatabaseFileLayout = new org.jdesktop.layout.GroupLayout(pnlDatabaseFile);
    pnlDatabaseFile.setLayout(pnlDatabaseFileLayout);
    pnlDatabaseFileLayout.setHorizontalGroup(
      pnlDatabaseFileLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(pnlDatabaseFileLayout.createSequentialGroup()
        .addContainerGap()
        .add(lblFilename)
        .add(22, 22, 22)
        .add(txtFilename, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(btnBrowseFile, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 35, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );
    pnlDatabaseFileLayout.setVerticalGroup(
      pnlDatabaseFileLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(pnlDatabaseFileLayout.createSequentialGroup()
        .addContainerGap()
        .add(pnlDatabaseFileLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblFilename)
          .add(btnBrowseFile)
          .add(txtFilename, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    databaseTypeGroup.add(radDatabaseServer);
    radDatabaseServer.setText("Database Server");
    radDatabaseServer.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    radDatabaseServer.setMargin(new java.awt.Insets(0, 0, 0, 0));
    radDatabaseServer.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        radDatabaseServerActionPerformed(evt);
      }
    });

    lblHost.setText("Host:");

    txtHost.setEnabled(false);

    lblPort.setText("Port:");

    txtPort.setEnabled(false);

    lblUser.setText("User:");

    txtUser.setEnabled(false);

    lblPassword.setText("Password:");

    txtPassword.setEnabled(false);

    org.jdesktop.layout.GroupLayout pnlDatabaseServerLayout = new org.jdesktop.layout.GroupLayout(pnlDatabaseServer);
    pnlDatabaseServer.setLayout(pnlDatabaseServerLayout);
    pnlDatabaseServerLayout.setHorizontalGroup(
      pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(pnlDatabaseServerLayout.createSequentialGroup()
        .addContainerGap()
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(lblHost)
          .add(lblPort)
          .add(lblUser)
          .add(lblPassword))
        .add(22, 22, 22)
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(txtPort, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
          .add(txtHost, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
          .add(txtUser, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
          .add(txtPassword, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE))
        .addContainerGap())
    );
    pnlDatabaseServerLayout.setVerticalGroup(
      pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(pnlDatabaseServerLayout.createSequentialGroup()
        .addContainerGap()
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblHost)
          .add(txtHost, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
          .add(lblPort)
          .add(txtPort, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblUser)
          .add(txtUser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(pnlDatabaseServerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblPassword)
          .add(txtPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addContainerGap(15, Short.MAX_VALUE))
    );

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(okButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cancelButton))
              .add(layout.createSequentialGroup()
                .add(lblName)
                .add(37, 37, 37)
                .add(txtName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE))
              .add(radDatabaseFile)))
          .add(layout.createSequentialGroup()
            .add(27, 27, 27)
            .add(pnlDatabaseFile, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(layout.createSequentialGroup()
                .add(17, 17, 17)
                .add(pnlDatabaseServer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              .add(radDatabaseServer))))
        .addContainerGap())
    );

    layout.linkSize(new java.awt.Component[] {cancelButton, okButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(lblName)
          .add(txtName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .add(33, 33, 33)
        .add(radDatabaseFile)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(pnlDatabaseFile, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(radDatabaseServer)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(pnlDatabaseServer, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 18, Short.MAX_VALUE)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(cancelButton)
          .add(okButton))
        .addContainerGap())
    );
    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void radDatabaseServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radDatabaseServerActionPerformed

   updateStates(!radDatabaseServer.isSelected());
    
  }//GEN-LAST:event_radDatabaseServerActionPerformed

  private void radDatabaseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radDatabaseFileActionPerformed

    updateStates(radDatabaseFile.isSelected());

  }//GEN-LAST:event_radDatabaseFileActionPerformed

  private void btnBrowseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseFileActionPerformed

    // Request file name from user
    JFileChooser fileChooser = new JFileChooser(".");
    int status = fileChooser.showOpenDialog(null);
    if (status == JFileChooser.CANCEL_OPTION ) {
      return;
    }      
    File selectedFile = fileChooser.getSelectedFile();

    // Create complete path
    String fileName = selectedFile.getAbsolutePath();
    txtFilename.setText(fileName);
  }//GEN-LAST:event_btnBrowseFileActionPerformed
  
  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
    
    if( this.radDatabaseServer.isSelected() ) {
      
      this.name = this.txtName.getText();
      this.host = this.txtHost.getText();
      this.port = Integer.parseInt(this.txtPort.getText());
      this.user = this.txtUser.getText();
      this.password = this.txtPassword.getPassword();
      this.databaseType = 1;
      
    } else {

      this.name = this.txtName.getText();
      this.file = this.txtFilename.getText();
      this.databaseType = 0;
    }
    
    doClose(RET_OK);
  }//GEN-LAST:event_okButtonActionPerformed
  
  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
    doClose(RET_CANCEL);
  }//GEN-LAST:event_cancelButtonActionPerformed
  
  /** Closes the dialog */
  private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
    doClose(RET_CANCEL);
  }//GEN-LAST:event_closeDialog
  
  private void doClose(int retStatus) {
    returnStatus = retStatus;
    setVisible(false);
    dispose();
  }

  private void updateStates(boolean fileSelected) {
    
    this.pnlDatabaseFile.setEnabled(fileSelected);
    this.txtFilename.setEnabled(fileSelected);

    this.pnlDatabaseServer.setEnabled(!fileSelected);
    this.txtHost.setEnabled(!fileSelected);
    this.txtPort.setEnabled(!fileSelected);
    this.txtUser.setEnabled(!fileSelected);
    this.txtPassword.setEnabled(!fileSelected);    

  }
  
  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new Db4oNewDatabaseDialog(new javax.swing.JFrame(), true).setVisible(true);
      }
    });
  }
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btnBrowseFile;
  private javax.swing.JButton cancelButton;
  private javax.swing.ButtonGroup databaseTypeGroup;
  private javax.swing.JLabel lblFilename;
  private javax.swing.JLabel lblHost;
  private javax.swing.JLabel lblName;
  private javax.swing.JLabel lblPassword;
  private javax.swing.JLabel lblPort;
  private javax.swing.JLabel lblUser;
  private javax.swing.JButton okButton;
  private javax.swing.JPanel pnlDatabaseFile;
  private javax.swing.JPanel pnlDatabaseServer;
  private javax.swing.JRadioButton radDatabaseFile;
  private javax.swing.JRadioButton radDatabaseServer;
  private javax.swing.JTextField txtFilename;
  private javax.swing.JTextField txtHost;
  private javax.swing.JTextField txtName;
  private javax.swing.JPasswordField txtPassword;
  private javax.swing.JTextField txtPort;
  private javax.swing.JTextField txtUser;
  // End of variables declaration//GEN-END:variables
  
  private int returnStatus = RET_CANCEL;

}
